package com.jic.member.impl;

import com.jic.common.base.vo.RestResult;
import com.jic.member.constant.enums.TripNodeTemplateTypeEnums;
import com.jic.member.mapper.TripNodeTemplateEntityMapper;
import com.jic.member.response.TripNodeTemplateEntityResponse;
import com.jic.member.response.TripNodeTemplateResponse;
import com.jic.member.service.TripNodeTemplateService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @author wwb
 * @date: 2020/6/5
 */
@Slf4j
@Service
public class TripNodeTemplateServiceImpl implements TripNodeTemplateService {

    @Resource
    private TripNodeTemplateEntityMapper tripNodeTemplateEntityMapper;

    @Override
    public RestResult<List<TripNodeTemplateEntityResponse>> getAllNodeTemplate() {
        try {
            List<TripNodeTemplateEntityResponse> response = new ArrayList<>();
            List<TripNodeTemplateResponse> allNodeTemplateList = tripNodeTemplateEntityMapper.getAllNodeTemplate();
            allNodeTemplateList.forEach(allNodeTemplate->{
               if (TripNodeTemplateTypeEnums.START.getName().equals(allNodeTemplate.getTemplateType())){
                   checkListIsPresence(response,TripNodeTemplateTypeEnums.START,allNodeTemplate);
               }else if(TripNodeTemplateTypeEnums.CROWD_CIRCLE.getName().equals(allNodeTemplate.getTemplateType())){
                   checkListIsPresence(response,TripNodeTemplateTypeEnums.CROWD_CIRCLE,allNodeTemplate);
               }else if(TripNodeTemplateTypeEnums.TRIGGERING_CONDITIONS.getName().equals(allNodeTemplate.getTemplateType())){
                   checkListIsPresence(response,TripNodeTemplateTypeEnums.TRIGGERING_CONDITIONS,allNodeTemplate);
               }else if(TripNodeTemplateTypeEnums.PERFORM_ACTION.getName().equals(allNodeTemplate.getTemplateType())){
                   checkListIsPresence(response,TripNodeTemplateTypeEnums.PERFORM_ACTION,allNodeTemplate);
               }
            });
            return RestResult.success(response);
        }catch (Exception e){
            log.error("查询节点模板列表错误====>>>>>>>>>>{}",e);
            return RestResult.error("9999","服务器错误，请稍后重试...");
        }
    }


    /**
     * 判断list是否存在
     */
    private void checkListIsPresence(List<TripNodeTemplateEntityResponse> templateEntityResponseList, TripNodeTemplateTypeEnums tripNodeTemplateTypeEnums, TripNodeTemplateResponse tripNodeTemplateResponse){
        if (CollectionUtils.isEmpty(templateEntityResponseList)){
            //list不存在值
            templateEntityResponseList.add(new TripNodeTemplateEntityResponse(){{
                setTemplateType(tripNodeTemplateTypeEnums.getDesc());
                setTripNodeTemplateList(new ArrayList<TripNodeTemplateResponse>(){{
                    add(tripNodeTemplateResponse);
                }});
            }});
        }else{
            //list存在值
            AtomicReference<Boolean> flag = new AtomicReference<>(true);
            templateEntityResponseList.stream().forEach(templateEntityResponse->{
                if (templateEntityResponse.getTemplateType().equals(tripNodeTemplateTypeEnums.getDesc())){
                    flag.set(false);
                    //相同类型 判断list是否有值
                    List<TripNodeTemplateResponse> tripNodeTemplateList = templateEntityResponse.getTripNodeTemplateList();
                    if (CollectionUtils.isEmpty(tripNodeTemplateList)){
                        templateEntityResponse.setTripNodeTemplateList(new ArrayList<TripNodeTemplateResponse>(){{
                            add(tripNodeTemplateResponse);
                        }});
                    }else{
                        templateEntityResponse.getTripNodeTemplateList().add(tripNodeTemplateResponse);
                    }
                }
            });
            if (flag.get()){
                templateEntityResponseList.add(new TripNodeTemplateEntityResponse(){{
                    setTemplateType(tripNodeTemplateTypeEnums.getDesc());
                    setTripNodeTemplateList(new ArrayList<TripNodeTemplateResponse>(){{
                        add(tripNodeTemplateResponse);
                    }});
                }});
            }
        }
    }




}
